# json模块用法
# https://www.runoob.com/python3/python3-json.html
# python遍历字典
# https://blog.csdn.net/whatday/article/details/100215800
# python with as语法
# https://blog.csdn.net/u010609757/article/details/102545112
import json

import requests

if __name__ == "__main__":
    # ua伪装
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62"
    }
    params = {
        "op": "keyword"
    }
    # ex: 武汉
    kw = input("enter a word:")
    data = {
        "cname": "",
        "pid": "",
        "keyword": kw,
        "pageIndex": "1",
        "pageSize": "0"
    }
    initResponse = requests.post("http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx",
                                 params=params,
                                 data=data,
                                 headers=headers)
    responseJson = initResponse.json()
    # print(responseJson['Table'])
    # print(responseJson['Table'][0])
    # print(responseJson['Table'][0]['rowcount'])
    data['pageSize'] = responseJson['Table'][0]['rowcount']

    finalResponse = requests.post("http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx",
                                  params=params,
                                  data=data,
                                  headers=headers)
    json.dump(finalResponse.json(), open("./file/" + "kfc.json", "w", encoding="utf-8"), ensure_ascii=False)
    print("over!!!")
